package com.atguigu.realtime.util;

import com.atguigu.realtime.bean.CartAddUuBean;
import com.atguigu.realtime.common.Constant;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lzc
 * @Date 2022/5/18 9:19
 */
public class JdbcUtil {
    public static Connection getPhoenixConnection(String driver,
                                                  String url) throws SQLException, ClassNotFoundException {
        
        return getJdbcConnection(driver, url, null, null);
    }
    
    public static Connection getJdbcConnection(String driver, String url, String user,
                                               String password) throws ClassNotFoundException, SQLException {
        // 1. 加载驱动
        Class.forName(driver);
        return DriverManager.getConnection(url, user, password);
        
    }
    
    
    public static void main(String[] args) throws SQLException, ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException {
        /*Connection conn = getPhoenixConnection(Constant.PHOENIX_DRIVER, Constant.PHOENIX_URL);
        List<JSONObject> list = queryList(conn, "select * from dim_spu_info where id=?", new Object[]{"11"}, JSONObject.class);
        for (JSONObject obj : list) {
    
            System.out.println(obj);
        }*/
        Connection conn = getJdbcConnection(Constant.CLICKHOUSE_DRIVER, Constant.CLICKHOUSE_URL_PRE + "gmall2022", null, null);
        List<CartAddUuBean> list = queryList(conn, "select * from dws_trade_cart_add_uu_window", null, CartAddUuBean.class);
        for (CartAddUuBean obj : list) {
            System.out.println(obj);
        }
    
    }
    /**
     * @param conn
     * @param querySql
     * @param args     长度意味着占位符的个数
     * @return
     */
    public static <T> List<T> queryList(Connection conn,
                                        String querySql,
                                        Object[] args,
                                        Class<T> tClass) throws SQLException, IllegalAccessException, InstantiationException, InvocationTargetException {
        // select * from t where id=?
        ArrayList<T> result = new ArrayList<>();
        System.out.println(querySql);
        PreparedStatement ps = conn.prepareStatement(querySql);
        // 给占位符赋值
        for (int i = 0; args != null && i < args.length; i++) {
            ps.setObject(i + 1,args[i]);
        }
    
    
        ResultSet resultSet = ps.executeQuery();
        // 获取查的表的元数据信息
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
    
        while (resultSet.next()) {
            // 创建一个T类型的对象, 每行封装到一个对象中
            T t = tClass.newInstance();
            // 有一行数据: 把这个一行数据的所有的列取出,封装到T类型的对象中
            for (int i = 1; i <=columnCount ; i++) {
                String name = metaData.getColumnLabel(i);
                Object value = resultSet.getObject(i);
    
                BeanUtils.setProperty(t, name, value);
            }
    
    
            result.add(t);
        }
    
    
        return result;
    }
}
